﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.Xml.Linq;

namespace SilverSmug
{
    public class SmugAcc
    {

        private static string apiUrl = "http://api.smugmug.com/services/api/rest/1.2.2/";
        public static string appKey = ApiKey.apiKey;
        public static string sessionID { get; set; }

        public static string loginMethod = "smugmug.login.anonymously";
        public static string getAlbumsMethod = "smugmug.albums.get";
        public static string getImagesMethod = "smugmug.images.get";
        public static string getImageInfoMethod = "smugmug.images.getInfo";

        public delegate void SMResponse(XDocument result);

        public static string makeRequestURL(String method, params string[] args) {
            StringBuilder req = new StringBuilder(apiUrl);
            req.Append("?method=").Append(method);

            foreach (String a in args)
            {
                req.Append("&"+a);
            }
        return req.ToString();
    }

        public static void sendRequest(string url, SMResponse handler)
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted +=  delegate(object sender, DownloadStringCompletedEventArgs a){
                handler(XDocument.Parse(a.Result));
            };
            client.DownloadStringAsync(new Uri(url));
        }

        public static string checkForError(XDocument xDoc)
        {
            XElement rsp = xDoc.Element("rsp");
            if(rsp.Attribute("stat").Value == "fail") {
                return xDoc.ToString();
            }
            return "";
            
        }
}
}
